#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/module.h>

#include <asm-generic/errno-base.h>
#include <uapi/asm-generic/errno.h>

static LIST_HEAD(my_list);

static LIST_HEAD(my_list2);

struct struct1 {
	struct list_head list;
	char name[20];
};

void inline queues01(void) {
	struct struct1 a0 = { .name = "a0", };
	struct struct1 a1 = { .name = "a1", };
	struct struct1 a2 = { .name = "a2", };
	struct struct1 a3 = { .name = "a3", };
	struct struct1 *pos;
	struct list_head;

	list_add_tail(&a0.list, &my_list);
	list_add_tail(&a1.list, &my_list);
	list_add_tail(&a2.list, &my_list);
	list_add_tail(&a3.list, &my_list);

	list_for_each_entry(pos, &my_list, list)
	{
		pr_err("name = %s\n", pos->name);
	}

}

void inline queues01_move(void) {
	struct struct1 a0 = { .name = "a0", };
	struct struct1 a1 = { .name = "a1", };
	struct struct1 a2 = { .name = "a2", };
	struct struct1 a3 = { .name = "a3", };

	struct struct1 a4 = { .name = "a4", };
	struct struct1 a5 = { .name = "a5", };
	struct struct1 a6 = { .name = "a7", };
	struct struct1 a7 = { .name = "a7", };

	struct struct1 *pos;
	struct list_head;

	list_add_tail(&a0.list, &my_list);
	list_add_tail(&a1.list, &my_list);
	list_add_tail(&a2.list, &my_list);
	list_add_tail(&a3.list, &my_list);

	list_add_tail(&a4.list, &my_list2);
	list_add_tail(&a5.list, &my_list2);
	list_add_tail(&a6.list, &my_list2);
	list_add_tail(&a7.list, &my_list2);

	list_for_each_entry(pos, &my_list, list)
	{
		pr_err("my_list1 name = %s\n", pos->name);
	}

	pr_err("#####################\n");

	list_for_each_entry(pos, &my_list2, list)
	{
		pr_err("my_list2 name = %s\n", pos->name);
	}

	list_move(&a2.list, &my_list2);

	pr_err("after move\n");

	list_for_each_entry(pos, &my_list, list)
	{
		pr_err("my_list1 name = %s\n", pos->name);
	}

	pr_err("#####################\n");

	list_for_each_entry(pos, &my_list2, list)
	{
		pr_err("my_list2 name = %s\n", pos->name);
	}

}

void inline queues01_del(void) {
	struct struct1 a0 = { .name = "a0", };
	struct struct1 a1 = { .name = "a1", };
	struct struct1 a2 = { .name = "a2", };
	struct struct1 a3 = { .name = "a3", };
	struct struct1 *pos;
	struct list_head;

	list_add_tail(&a0.list, &my_list);
	list_add_tail(&a1.list, &my_list);
	list_add_tail(&a2.list, &my_list);
	list_add_tail(&a3.list, &my_list);

	list_del(&a2.list);

	list_for_each_entry(pos, &my_list, list)
	{
		pr_err("name = %s\n", pos->name);
	}

	pr_err("list_empty = %d\n", list_empty(&my_list));
}

void inline stack01(void) {
	struct struct1 a0 = { .name = "a4", };
	struct struct1 a1 = { .name = "a5", };
	struct struct1 a2 = { .name = "a6", };
	struct struct1 a3 = { .name = "a7", };
	struct struct1 *pos;
	struct list_head;

	list_add(&a0.list, &my_list);
	list_add(&a1.list, &my_list);
	list_add(&a2.list, &my_list);
	list_add(&a3.list, &my_list);

	list_for_each_entry(pos, &my_list, list)
	{
		pr_err("name = %s\n", pos->name);
	}
}

int myinit(void) {
//	queues01();
//	stack01();
//	queues01_del();
	queues01_move();
	return 0;
}

static __init int my_init(void) {
	return  myinit();

}

static void __exit my_exit(void) {

}

module_init(my_init);
module_exit(my_exit);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Andy");
MODULE_DESCRIPTION("A sample c  driver");
